<?php
declare (strict_types = 1);

namespace app\sms\controller;

use app\BaseController;
use app\Request;
use app\sms\model\SmsTemplateModel;
use app\sms\service\SmsService;
use think\facade\View;

class Admin extends BaseController
{
    /**
     * 短信平台列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @return void
     */
    public function platform(Request $request){
        if($request->param('_action') == 'getList'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $adminList = SmsService::getPlatformList($params,$page,$limit);
            return json($adminList);
        }
        return View::fetch('platform',[
           
        ]);
    }

    /**
     * 获取模板列表
     *
     * @Author tanruizheng
     * @DateTime 2021-08-30
     * @param Request $request
     * @return void
     */
    public function template(Request $request){
        if($request->param('_action') == 'getList'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $list = SmsService::getTemplateList($params, $page, $limit);
            return json($list);
        }
        return View::fetch('template',[
           
        ]);
    }

   /**
     * 添加模板
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function addTemplate(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = SmsService::addOrEditTemplate($params);
            return json($result);
        }
        return View::fetch('addTemplate',[
           
        ]);
    }

    /**
     * 编辑模板
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function editTemplate(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = SmsService::addOrEditTemplate($params);
            return json($result);
        }
        $id = $request->param('id',0);
        $data = SmsTemplateModel::where('id',$id)->find()->toArray();
        return View::fetch('editTemplate',[
            'data' => $data
        ]);
    }

    /**
     * 删除模板
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function templateDelete(Request $request){
        $id = $request->param('id',0);
        if(!$id){
            return self::createFail('参数错误');
        }
        $result = SmsService::templateDelete($id);
        return json($result);
    }

    /**
     * 获取短信日志
     *
     * @Author tanruizheng
     * @DateTime 2021-08-30
     * @param Request $request
     * @return void
     */
    public function smsLog(Request $request){
        if($request->param('_action') == 'getList'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $list = SmsService::getLogList($params, $page, $limit);
            return json($list);
        }
        return View::fetch('smsLog',[
           
        ]);
    }
}
